import {
  reviewList
} from '../../../api/review.js'
const util = require('../../../utils/util.js')
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentsData: [],
    meta: [],
    metaType: false,
    pageNum: 1,
    pageSize: 10,
    length: 0,
    id: '',
    icon: [false, false, false, false, false],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.id = options.id
    this.loadList()
  },
  loadList() {
    var _this = this
    reviewList({
      productId: this.data.id,
      pageSize: _this.data.pageSize,
      pageNum: _this.data.pageNum,
    }).then(res => {
      console.log(res)
      if (res.type == 'success') {
        res.data.data.forEach(function(item) {
          if (!util.isNull(item.meta)) {
            item.meta = JSON.parse(item.meta)
          }
          item.createDate = util.datetimeFormat(item.createDate)
          _this.data.commentsData.push(item)
        })
        _this.data.length = res.data.data.length
        _this.setData({
          commentsData: _this.data.commentsData
        })
      }
    }).catch(error => {
      wx.showToast({
              icon: "none",
        title: error.content,
      })
    })
  },
  metaClick(data) {
    console.log(data)
    var _this = this
    _this.data.meta = data.currentTarget.dataset.item;
    _this.data.metaType = true
    _this.setData({
      meta: _this.data.meta,
      metaType: _this.data.metaType
    })
  },
  cancelClick() {
    var _this = this
    _this.data.metaType = false
    _this.setData({
      metaType: _this.data.metaType
    })
  },
  onReachBottom: function() {
    if (this.data.length >= 10) {
      this.data.pageNum += 1
      this.loadList()
      this.setData({
        pageNum: this.data.pageNum
      })
    }
  },
  downRefresh: function() {
    this.data.commentsData = []
    this.data.pageSize = 10
    this.data.pageNum = 1
    this.loadList()
    this.setData({
      commentsData: this.data.commentsData,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    })
  },
})